Skip to main content
ICT
Lesson A10 - The String Class
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

F. String Translation Methods page 8 of 17

Translate Method
Sample Syntax
String toLowerCase(); String greeting = "Hi World!";
greeting = greeting.toLowerCase();
// greeting <- "hi world!"
String toUpperCase(); String greeting = "Hi World!";
greeting = greeting.toUpperCase();
// greeting <- "HI WORLD!"
String trim(); String needsTrim = " trim me! ";
needsTrim = needsTrim.trim();
// needsTrim <- "trim me!"
String substring(int beginIndex) String sample = "hamburger";
sample = sample.substring(3);
// sample <- "burger"
String substring(int beginIndex, int endIndex) String sample = "hamburger";
sample = sample.substring(4, 8);
// sample <- "urge"
  1. toLowerCase() returns a String with the same characters as the String object, but with all characters converted to lowercase. Notice that in all of the above samples, the String object is placed on the left hand side of the assignment statement. This is necessary because Strings in Java are immutable. Please see section G for a full explanation of immutable.

  2. toUpperCase() returns a String with the same characters as the String object, but with all characters converted to uppercase.

  3. trim() returns a String with the same characters as the String object, but with the leading and trailing whitespace removed.

  4. substring(int beginIndex) returns the substring of the String object starting from beginIndex through to the end of the String object.

  5. substring(int beginIndex, int endIndex) returns the substring of the String object starting from beginIndex through, but not including, position endIndex of the String object. That is, the new String contains characters numbered beginIndex to endIndex-1 in the original String.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.